package com.okq.service.Impl;

import com.okq.dao.CustomerDao;
import com.okq.dao.LogisticsDao;
import com.okq.entity.Customer;
import com.okq.entity.Logistics;
import com.okq.service.ILogisticsService;
import com.okq.util.page.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 描述 : 物流接口实现类
 * 作者 : zdl
 * 日期 : 2017/6/29 15:10
 */
@Transactional
@Service
public class LogisticsServiceImpl implements ILogisticsService {

    @Autowired
    private LogisticsDao logisticsDao;

    @Autowired
    private CustomerDao customerDao;

    @Override
    public void saveLogistics(Logistics logistics) {
        logisticsDao.saveLogistics(logistics);
    }

    @Override
    public void deleteLogistics(Logistics logistics) {
        logisticsDao.deleteLogistics(logistics);
    }

    @Override
    public void updateLogistics(Logistics logistics) {
        logisticsDao.updateLogistics(logistics);
    }

    @Override
    public void saveOrUpdateLogistics(Logistics logistics) {
        logisticsDao.saveOrUpdateLogistics(logistics);
    }

    @Override
    public Logistics getLogisticsById(String id) {
        return logisticsDao.getLogisticsById(id);
    }

    @Override
    public List<Logistics> listAllLogistics() {
        return logisticsDao.listAllLogistics();
    }

    @Override
    @SuppressWarnings("all")
    public Page<Logistics> pageByLogistics(Page<Logistics> page, Logistics logistics) {
        if (logistics != null) {
            String keyword = logistics.getKeyword();
            if (StringUtils.isNotBlank(keyword)) {
                List<Customer> cList = customerDao.listCustomerByKeyword(keyword);
                if (cList != null && !cList.isEmpty() && cList.size() > 0) {
                    String customerIds = "";
                    for (Customer c : cList) {
                        customerIds = c.getId() + ",";
                    }
                    if (StringUtils.isNotBlank(customerIds)) {
                        customerIds = customerIds.substring(0, (customerIds.length() - 1));
                        logistics.setCustomerIds(customerIds);
                    }
                }
            }
        }
        return logisticsDao.pageByLogistics(page, logistics);
    }
}
